package cn.enjoy.rabbitmq.handler.original;

import com.rabbitmq.client.*;
import lombok.SneakyThrows;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author K
 * @date 2020/11/26 10:27
 */
public class FanoutConsumer2 {
    //交换机名称
    static final String FANOUT_EXCHAGE = "fanout_exchange";
    //队列名称
    static final String FANOUT_QUEUE_1 = "fanout_queue_1";
    //队列名称
    static final String FANOUT_QUEUE_2 = "fanout_queue_2";

    @SneakyThrows
    public static void main(String[] args) {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(FANOUT_EXCHAGE, BuiltinExchangeType.FANOUT);

        channel.queueDeclare(FANOUT_QUEUE_2, true, false, false, null);
        channel.queueBind(FANOUT_QUEUE_2, FANOUT_EXCHAGE, "");
        channel.basicQos(1);

        channel.basicConsume(FANOUT_QUEUE_2, false, new DefaultConsumer(channel) {
            @SneakyThrows
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                super.handleDelivery(consumerTag, envelope, properties, body);
                System.out.println("消费者2：" + new String(body, StandardCharsets.UTF_8));
                channel.basicAck(envelope.getDeliveryTag(), false);
                Thread.sleep(1000);
            }
        });
    }

}
